﻿namespace ImageProcessing
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;

    /// <summary>
    /// Video capture cpp class
    /// </summary>
    public static class PHVideoCapture
    {
        const string dllLocation = "PHVideoCapturing.dll";

        [DllImport(dllLocation, EntryPoint = "PHInitializeCapture", CallingConvention = CallingConvention.Cdecl)]
        public static extern void PHInitializeCapture(char[] pathAddress, ref int width, ref int height, ref int numberOfFrames, ref int captureInitialized);

        [DllImport(dllLocation, EntryPoint = "PHInitializeWriter", CallingConvention = CallingConvention.Cdecl)]
        public static extern void PHInitializeWriter(char[] pathAddress, int width, int height);

        [DllImport(dllLocation, EntryPoint = "PHCaptureFrame", CallingConvention = CallingConvention.Cdecl)]
        public static extern void PHCaptureFrame(int bmpPointer, int extraBytes);

        [DllImport(dllLocation, EntryPoint = "PHWriteFrame", CallingConvention = CallingConvention.Cdecl)]
        public static extern void PHWriteFrame(int bmpPointer, int extraBytes);

        [DllImport(dllLocation, EntryPoint = "PHSeekVideo", CallingConvention = CallingConvention.Cdecl)]
        public static extern void PHSeekVideo(double frameIndex);

        [DllImport(dllLocation, EntryPoint = "ReleaseWriter", CallingConvention = CallingConvention.Cdecl)]
        public static extern void ReleaseWriter();

        [DllImport(dllLocation, EntryPoint = "ReleaseVideoCapture", CallingConvention = CallingConvention.Cdecl)]
        public static extern void ReleaseVideoCapture();

        [DllImport(dllLocation, EntryPoint = "DetectFaces", CallingConvention = CallingConvention.Cdecl)]
        public static extern void DetectFaces(int bmpPointer, int extraBytes);

        [DllImport(dllLocation, EntryPoint = "PHInitializeCameraCapture", CallingConvention = CallingConvention.Cdecl)]
        public static extern void PHInitializeCameraCapture(ref int width, ref int height, ref int cameraInitialized);

        public static void RecordFrame(Bitmap bmp)
        {
            BitmapData bmpd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            int extraBytes = bmpd.Stride - (bmpd.Width * 3);

            PHWriteFrame((int)bmpd.Scan0, extraBytes);

            bmp.UnlockBits(bmpd);
        }

        public static void QueryFrame(Bitmap bmp)
        {
            BitmapData bmpd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            int extraBytes = bmpd.Stride - (bmpd.Width * 3);

            PHCaptureFrame((int)bmpd.Scan0, extraBytes);

            bmp.UnlockBits(bmpd);
        }
    }
}